const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : `0${n}`
}

const showToast = (message, onSuccess, onFail, duration = 2000, mask = false) => {
    wx.showToast({
        title: message,
        icon: 'none',
        duration,
        mask,
        success: () => {
            setTimeout(() => {
                if (typeof onSuccess === 'function') {
                    onSuccess()
                }
            }, duration)
        },
        fail: () => {
            if (typeof onFail === 'function') {
                onFail()
            }
        }
    })
}

const showModal = (message, onConfirm, onCancel) => {
    wx.showModal({
        title: '确认操作',
        content: message,
        complete: (res) => {
            if (res.cancel) {
                onCancel()
            }

            if (res.confirm) {
                onConfirm()
            }
        }
    })
}
import {
    base64Decode
} from '../utils/base64';

function getParams(params) {
    try {
        // 如果 params 为空，则直接提示并返回
        if (!params) {
            return;
        }

        // Base64 解码
        const decodedString = base64Decode(params);
        if (!decodedString) {
            return;
        }

        // JSON 解析
        const decodedParams = JSON.parse(decodedString);

        return decodedParams;
    } catch (error) {
        console.error('onLoad 解密或解析参数出错:', error);
    }
}

const isProduction = false;

const logger = {
    log: (...args) => {
        if (!isProduction) {
            console.log('[LOG]:', ...args);
        }
    },
    warn: (...args) => {
        if (!isProduction) {
            console.warn('[WARN]:', ...args);
        }
    },
    error: (...args) => {
        if (!isProduction) {
            console.error('[ERROR]:', ...args);
        }
    },
    info: (...args) => {
        if (!isProduction) {
            console.info('[INFO]:', ...args);
        }
    }
};

module.exports = {
    formatTime,
    showToast,
    showModal,
    getParams,
    logger
}